package com.zhao.dota.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.dota.model.League;
import com.zhao.dota.model.UserHero;
import com.zhao.dota.service.LeagueService;
import com.zhao.dota.service.UserHeroService;
import com.zhao.dota.struct.Result;
import com.zhao.dota.vo.UserAchievementVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/league")
public class LeagueController {

    @Resource
    private LeagueService leagueService;
    @Resource
    private UserHeroService userHeroService;

    @GetMapping("/")
    public Result<Page<League>> page(Page<League> page) {
        page.addOrder(OrderItem.desc("end_time"));
        Page<League> data = leagueService.page(page);
        return Result.success(data);
    }

    @GetMapping("/{id}")
    public Result<League> one(@PathVariable Integer id) {
        League league = leagueService.getById(id);
        return Result.success(league);
    }

    @PostMapping("/")
    public Result<Void> save(League league) {
        int i = leagueService.saveLeague(league);
        return Result.complete(i > 0);
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        boolean b = leagueService.removeById(id);
        return Result.complete(b);
    }

    @GetMapping("/details")
    public Result<?> details(Integer id) {
        return Result.success();
    }

    @GetMapping("/userHeroList")
    public Result<List<UserHero>> userHeroList(Integer id) {
        LambdaQueryWrapper<UserHero> query = Wrappers.lambdaQuery(UserHero.class)
                .eq(UserHero::getLeagueId, id)
                .eq(UserHero::getLost, false);
        List<UserHero> list = userHeroService.list(query);
        return Result.success(list);
    }

    @GetMapping("/userAchievement")
    public Result<List<UserAchievementVo>> userAchievement(Integer id) {
        List<UserAchievementVo> list = userHeroService.achievements(id);
        return Result.success(list);
    }

    @PostMapping("/removeHero")
    public Result<Boolean> removeHero(Integer id, Integer userId, Integer heroId) {
        boolean success = userHeroService.remove(id, userId, heroId);
        return Result.complete(success);
    }

}
